package com.app.service.authority;

import com.app.entity.Pagination;
import com.app.entity.authority.Dictionary;
import com.app.mapper.authority.DictionaryMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DictionaryService {
    private final static Logger logger = LoggerFactory.getLogger(DictionaryService.class);

    @Autowired
    DictionaryMapper dictionartMapper;

    //查    data-info列表
    public Pagination getInfo(Pagination page) {
        logger.info("func:getInfo %n page:" + page);

        //排序名
        if (page.getOrderName() == null) {
            page.setOrderName("MODIFYTIME");
        }
        //排序方式
        if (page.getOrderKind() == null) {
            page.setOrderKind("desc");
        }

        ObjectMapper mapper = new ObjectMapper();
        String jsonStr = "";
        String searchName = "";

        if (page.getSearchData() != null) {
            try {
                jsonStr = mapper.writeValueAsString(page.getSearchData());
                Dictionary r = mapper.readValue(jsonStr, Dictionary.class);
                searchName = r.getName();
            } catch (Exception e) {
                logger.error("func:getInfo %n jsonStr:" + jsonStr + "%n" + e.getMessage());
            }
        }

        List<Dictionary> list = dictionartMapper.getInfo(page.getCurrentItem()-1, page.getPageSize(), searchName, page.getOrderName(), page.getOrderKind());
        page.setData(list);

        page.setTotalItems(dictionartMapper.getListSize(page.getCurrentItem(), page.getPageSize(), searchName));

        return page;
    }

    //增
    public int insert(Dictionary _data) {
        logger.info("func:insert %n _data:" + _data.toString());
        return dictionartMapper.insert(_data);
    }

    //根据ID获取list
    public List<Dictionary> getList(Dictionary _data) {
        logger.info("func:getList %n _data:" + _data);
        List<Dictionary> dList = dictionartMapper.getList(_data);
        return dList;
    }

    //改
    public int updateList(Dictionary _data) {
        logger.info("func:updateList %n _data:" + _data);
        return dictionartMapper.updateList(_data);
    }

    //删
    public int delList(Dictionary _data) {
        logger.info("func:delList %n _data:" + _data);
        return dictionartMapper.delList(_data);
    }

    //查重复CODE
    public List<Dictionary> validateLevelCode(Dictionary _data) {
        logger.info("func:validateLevelCode %n _data:" + _data);
        List<Dictionary> dList = dictionartMapper.validateLevelCode(_data);
        return dList;
    }
}
